package com.zhijian.medical.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.zhijian.BusinessException;
import com.zhijian.medical.dao.local.DeviceMapper;
import com.zhijian.medical.entity.dto.PersonDto;
import com.zhijian.medical.entity.po.Device;
import com.zhijian.medical.entity.po.DeviceAdjustRecord;
import com.zhijian.medical.entity.po.DeviceRepairHistory;
import com.zhijian.medical.entity.vo.request.DeviceAdjustRecordSearchVo;
import com.zhijian.medical.entity.vo.request.DeviceSearchVo;
import com.zhijian.medical.entity.vo.request.RoutinePatientSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.DeviceAdjustRecordVo;
import com.zhijian.medical.entity.vo.response.DeviceTsVo;
import com.zhijian.medical.entity.vo.response.DeviceVo;
import com.zhijian.medical.util.AssertUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PatientDeviceService {

    @Autowired
    private DeviceService service;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private DeviceRepairHistoryService deviceRepairHistoryService;

    @Autowired
    private DeviceAdjustRecordService deviceAdjustRecordService;

    @Autowired
    private DeviceAdjustRecordService adjustRecordService;

    public List<DeviceTsVo> selectDeviceByDevice(String deviceCode, String typeStr){
        //List<DeviceVo> deviceVos = service.selectList(DeviceSearchVo.builder().hospitalNumber(deviceCode).build());
        List<Device> deviceVos;
        if (StringUtils.hasText(deviceCode)) {
            deviceVos = deviceMapper.selectListByTypeAndHospitalNumber(typeStr, deviceCode);
        }else {
            deviceVos = deviceMapper.selectListByType(typeStr);
        }
        List<DeviceTsVo> deviceVoResult = new ArrayList<>();
        for (Device device : deviceVos) {
            DeviceTsVo deviceTsVo = new DeviceTsVo();
            BeanUtils.copyProperties(device, deviceTsVo);
            // 默认维修时间是现在时间- 3 个月
            Date adjustDate = DateUtil.offset(new Date(), DateField.MONTH, -3);
            String adjustTime = DateUtil.formatDateTime(adjustDate);
            // 默认下次维修时间是现在时间+8个月
            adjustDate = DateUtil.offset(new Date(), DateField.MONTH, 8);
            String nextAdjustTime = DateUtil.formatDateTime(adjustDate);
            // 查找维修记录
            List<DeviceAdjustRecordVo> deviceAdjustRecordVos = adjustRecordService.selectList(DeviceAdjustRecordSearchVo.builder().deviceId(deviceTsVo.getId()).build());
            // 有记录
            if (CollUtil.isNotEmpty(deviceAdjustRecordVos)) {
                //adjustTime = deviceAdjustRecordVos.get(0).getCreateTime();
                adjustTime = deviceAdjustRecordVos.get(0).getDate() + " 00:00:00";
                adjustDate = DateUtil.parse(adjustTime);
                nextAdjustTime = DateUtil.formatDateTime(DateUtil.offset(adjustDate, DateField.MONTH, 11));
            }
            deviceTsVo.setJzrq(adjustTime);
            deviceTsVo.setXcjzrq(nextAdjustTime);
            deviceVoResult.add(deviceTsVo);
        }
        return deviceVoResult;
    }

    /**
     * 检修历史
     * @param deviceChangeEntity 设备信息
     */
    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    public void changeState(RoutinePatientSaveOrUpdateVo.DeviceChangeEntity deviceChangeEntity){
        service.updateStatus(deviceChangeEntity.getId(), deviceChangeEntity.getStatus());
        if ("检修".equals(deviceChangeEntity.getStatus())){
            Device device = service.selectById(deviceChangeEntity.getId());
            //需要添加维修历史
            DeviceRepairHistory deviceRepairHistory = new DeviceRepairHistory();
            deviceRepairHistory.setDeviceId(deviceChangeEntity.getId());
            deviceRepairHistory.setDeviceName(device.getName());
            deviceRepairHistory.setZyz(device.getProfessionGroup());
            deviceRepairHistory.setCode(device.getCode());
            deviceRepairHistory.setCode2(device.getCode2());
            deviceRepairHistory.setExecuteDate(device.getExecuteDate());
            deviceRepairHistory.setPublishDate(device.getPublishDate());
            deviceRepairHistory.setZrr(device.getZrr());
            deviceRepairHistory.setVersion(device.getVersion());
            deviceRepairHistory.setSaveTime(device.getSaveTime());
            deviceRepairHistory.setDw(device.getDw());
            deviceRepairHistoryService.insert(deviceRepairHistory);
        }
    }

    /**
     * 校准时间
     * @param deviceChangeEntity 设备信息
     */
    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    public void adjustDate(PersonDto personDto, RoutinePatientSaveOrUpdateVo.DeviceChangeEntity deviceChangeEntity){
        DeviceAdjustRecord deviceAdjustRecord = new DeviceAdjustRecord();
        deviceAdjustRecord.setDeviceId(deviceChangeEntity.getId());
        deviceAdjustRecord.setDate(deviceChangeEntity.getAdjustTime());
        deviceAdjustRecord.setResult("合格");
        deviceAdjustRecord.setInstitutionId(41L);
        deviceAdjustRecord.setUserName(personDto.getName());
        deviceAdjustRecordService.insert(deviceAdjustRecord);
    }
}
